using System;
using System.Collections.Generic;
using ZFramework;

namespace ZerAx.Roguelike
{
    public class Server : IServer
    {
        private Logic _logic;

        private int idCounter = 1;

        private Dictionary<int, IClientConn> connections = [];

        public Server()
        {
            _logic = new();
        }

        public void AddConnection(IClientConn conn)
        {
            connections.Add(idCounter++, conn);
        }

        public void RemoveConnection(int id)
        {
            connections.Remove(id);
        }

        public void Broadcast(IGameEvent e)
        {
            foreach (var conn in connections.Values)
            {
                conn.Send(e);
            }
        }

        public void SendTo(int connId, IGameEvent e)
        {
            connections[connId].Send(e);
        }

        public void SendFor(Func<int, IGameEvent> op)
        {
            foreach (var (id, conn) in connections)
            {
                conn.Send(op(id));
            }
        }
    }
}